/*
 Data Loader for spreadsheet under GNU general public liscence
 */
/**
 *
 *  @author lanchana
 *  @ DataLoader for spreadsheet
 *  @ email lanch.gunawardhana@email.com
 */
package forms;

import java.util.ArrayList;
import java.util.List;


public class DataManupulator {

    List setTableData(ArrayList<String> fields, ArrayList<Object[]> data) {
            List dataz = new ArrayList();//The actual data to be added to table
            Object []colFields=new Object[fields.size()+1];//the columns with the checkbox column
            colFields[0]=Boolean.FALSE;//first column is set to checkbox column
            for(int i=0;i<fields.size();i++){
              colFields[i+1]=fields.get(i);
            }
            dataz.add(colFields);//field names are added
            for (int i = 0; i < data.size(); i++) {
            Object record[] = new Object[fields.size()+1];
            record[0] = Boolean.FALSE;
            Object []arr=data.get(i);//consist of one row
            for (int j = 0; j < fields.size(); j++) {
              System.out.println(arr[j]);
              record[j+1] = arr[j];
            }
            dataz.add(record);//total rows are added
           }
             return dataz;//return the total table data
    }

    Object[][] getTotAray(ArrayList<Object[]> data, ArrayList<String> fields) {
        Object [][]o=new Object [data.toArray().length][fields.toArray().length];//this part is to load the whole table according to user preference

              for(int i=0;i<data.toArray().length;i++){
                   for(int j=0;j<fields.toArray().length;j++){
                  if(i==0){
                      o[i][j]=fields.get(j);

                  }
                  else{
                      Object []da=data.get(i);
                      o[i][j]=da[j];


                }
                  }
                     }
        return o;//return total data in the jtable
    }

    Object[][] getSelectedArray(ArrayList<ArrayList> rowData, ArrayList<Object> row) {
        int a=rowData.size();
            int b=row.size();
            Object [][] xdata=new Object[a][b];
            for(int i=0;i<a;i++){
                            java.util.ArrayList<Object>datas=rowData.get(i);
                            for(int j=0;j<b;j++){
                               System.out.print(datas.get(j).toString()+" ");
                               xdata[i][j]=datas.get(j);
                               System.out.print(xdata[i][j]);

                            }
                       System.out.println("\n");
                        }
            return xdata;
    }

}//return the selected data from the jtable
